package com.fjh.springboot_01.mapper;

import com.fjh.springboot_01.pojo.Announcement;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface AnnouncementMapper {
    
    @Insert("INSERT INTO announcement (title, content, create_user, create_user_name, create_time, update_time, status) " +
            "VALUES (#{title}, #{content}, #{createUser}, #{createUserName}, NOW(), NOW(), #{status})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void insert(Announcement announcement);
    
    @Select("SELECT * FROM announcement WHERE id = #{id}")
    Announcement findById(Integer id);
    
    @Select("SELECT * FROM announcement WHERE status = 1 ORDER BY create_time DESC")
    List<Announcement> findPublished();
    
    @Select("SELECT * FROM announcement ORDER BY create_time DESC")
    List<Announcement> findAll();
    
    @Update("UPDATE announcement SET title = #{title}, content = #{content}, update_time = NOW(), status = #{status} " +
            "WHERE id = #{id}")
    void update(Announcement announcement);
    
    @Delete("DELETE FROM announcement WHERE id = #{id}")
    void delete(Integer id);
} 